home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / html / psfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-24  |  1.6 KB  |  78 lines

  1. /*  $Id: psfile.c,v 1.2 1998/03/24 13:38:09 jan Exp $
  2.  
  3.     Designed and implemented by Jan Wielemaker
  4.     E-mail: jan@swi.psy.uva.nl
  5.  
  6.     Copyright (C) 1996 University of Amsterdam. All rights reserved.
  7. */
  8.  
  9. #include <SWI-Prolog.h>
  10. #include <stdio.h>
  11.  
  12. #define MAXLINE 10240
  13.  
  14. static int
  15. substr(const char *in, const char *sub)
  16. { int ls = strlen(sub);
  17.   int l = strlen(in) - ls;
  18.  
  19.   for( ; l >= 0; in++, l-- )
  20.   { if ( strncmp(in, sub, ls) == 0 )
  21.       return 1;
  22.   }
  23.  
  24.   return 0;
  25. }
  26.  
  27.  
  28. static foreign_t
  29. pl_get_ps_parameters(term_t file, term_t iseps, term_t bb)
  30. { char *fname;
  31.   FILE *fd;
  32.  
  33.   if ( !PL_get_chars(file, &fname, CVT_ALL) )
  34.     return PL_warning("get_ps_parameters/3: invalid filename");
  35.  
  36.   if ( (fd = fopen(fname, "r")) )
  37.   { char buf[MAXLINE];
  38.     char *s;
  39.  
  40.     if ( (s=fgets(buf, sizeof(buf), fd)) )
  41.     { if ( substr(s, "EPSF") )
  42.     PL_unify_atom_chars(iseps, "eps");
  43.       else
  44.     PL_unify_atom_chars(iseps, "ps");
  45.     }
  46.     
  47.     do
  48.     { double a1, a2, a3, a4;
  49.  
  50.       if ( sscanf(buf, "%%%%BoundingBox: %lf %lf %lf %lf", &a1, &a2, &a3, &a4) == 4 )
  51.       { fclose(fd);
  52.     return PL_unify_term(bb,
  53.                  PL_FUNCTOR, PL_new_functor(PL_new_atom("bb"), 4),
  54.                  PL_FLOAT, a1,
  55.                  PL_FLOAT, a2,
  56.                  PL_FLOAT, a3,
  57.                  PL_FLOAT, a4);
  58.       }
  59.     } while( (s=fgets(buf, sizeof(buf), fd)) );
  60.  
  61.     fclose(fd);
  62.     PL_warning("get_ps_parameters/3: could not find %%%%BoundingBox in %s",
  63.            fname);
  64.  
  65.     PL_fail;
  66.   }
  67.  
  68.   PL_warning("get_ps_parameters/3: could not open %s", fname);
  69.   PL_fail;
  70. }
  71.  
  72.  
  73. void
  74. install_ps()
  75. { PL_register_foreign("get_ps_parameters", 3, pl_get_ps_parameters, 0);
  76. }
  77.  
  78.